<template>
  <div ref="wrapper_hook" class="wrapper"/>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'financialBar',
  props: {
    seriesName: {
      type: String,
      default: '订单'
    },
    source: {
      type: Object,
      default: () => {
        return {
          xAxisData:[],
          done:[],
          notDone:[],
        }
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    source(newValue) {
      this._initECharts(newValue)
    }
  },
  mounted() {
    this._initECharts(this.source)
    window.addEventListener('resize', this.resize)
    this.$once('hook:beforeDestroy', () => {
      window.removeEventListener('resize', this.resize)
    })
  },
  destroyed() {
    this.chart.dispose()
  },
  methods: {
    _initECharts(data) {
      this.chart ? this.update(data) : this.draw(data)
    },
    resize(delay = 400) {
      setTimeout(() => {
        this.chart && this.chart.resize()
      }, delay)
    },
    draw(data) {
      const {xAxisData,done,notDone} = data
      this.chart = echarts.init(this.$refs.wrapper_hook)

      const options = {
        toolbox: {
          show: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['已办', '未办'],
          icon:'circle',
          left:'center',
          top: 24,
        },
        grid: {
          top: 70,
          left: 30,
          right: 0,
          bottom: 20
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            fontsize: 14
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#575656'
            }
          },
          axisTick: {
            show: false
          }
        },
        xAxis: {
          type: 'category',
          data: xAxisData,
          axisLabel: {
            fontsize: 14,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#575656'
            }
          }
        },
        series: [
          {
            name: '已办',
            type: 'bar',
            stack: '已办',
            emphasis: {
              focus: 'series'
            },
            barWidth: 40,
            data: done,
            color: '#28DC32FF',
            itemStyle: {
              color:  () => {
                return '#28DC32FF'
              }
            }
          },
          {
            name: '未办',
            type: 'bar',
            stack: '已办',
            emphasis: {
              focus: 'series'
            },
            barWidth: 50,
            data: notDone,
            color: 'rgba(249,193,78,0.5)',
            itemStyle: {
              color: () => {
                return 'rgba(249,193,78,0.5)'
              },
            },
            label: {
              normal: {
                show: true,
                formatter: params => {
                  return `${params.data}/${done[params.dataIndex]}`
                },
                color:'red',
                position: "top",
                textStyle: {
                  fontSize: 13,
                  color: "#787878",
                }
              }
            }
          },
        ]
      }
      this.chart.setOption(options)
      this.chart.on('click', params => {
        this.$emit('change', params)
      })
    },
    async update(data) {
      if (!this.chart) return
      const options = this.chart.getOption()

      const {xAxisData,done,notDone} = data
      options.xAxis[0].data = xAxisData
      options.series[0].data = done
      options.series[1].data = notDone
      options.series[1].label.formatter = params => {
        return `${params.data}/${done[params.dataIndex]}`
      }

      await this.$nextTick()
      this.chart.setOption(options, true)
    }
  }
}
</script>

<style scoped>
.wrapper {
  width: 100%;
  min-height: 280px;
}
</style>

<!--

option = {

    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: [
        {
            type: 'category',
            data: ['SUV', '轿车']
        }
    ],
    yAxis: [
        {
            type: 'value'
        }
    ],
    series: [





    ]
};
-->
